package com.smartcommunity.kafka.config;

import com.smartcommunity.utils.KafkaKeyManage;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
import org.springframework.kafka.config.KafkaListenerContainerFactory;
import org.springframework.kafka.core.*;
import org.springframework.kafka.listener.ConcurrentMessageListenerContainer;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class StaticticsConsumerConfig {

    @Value("${kafka.business.bootstrap-servers}")
    private String servers;
    @Value("${kafka.business.consumer.enable.auto.commit}")
    private boolean enableAutoCommit;
    @Value("${kafka.business.consumer.session-timeout}")
    private String sessionTimeout;
    @Value("${kafka.business.consumer.auto-commit-interval-ms}")
    private String autoCommitInterval;
    @Value("${kafka.business.consumer.auto-offset-reset}")
    private String autoOffsetReset;

    @Value("${kafka.statistics.consumer.group:group_statistics1}")
    private String groupId;

    /**
     * kafka开启账号密码访问需要的keyStore的地址
     */
    @Value("${kafka.business.keyStoreUrl:}")
    private String keyStoreUrl;

    @Value("${kafka.business.keyStorePassword:}")
    private String keyStorePassword;

    @Value("${kafka.business.keyPassword:}")
    private String keyPassword;

    /**
     * kafka开启账号密码访问需要的trustStore的地址
     */
    @Value("${kafka.business.trustStoreUrl:}")
    private String trustStoreUrl;

    @Value("${kafka.business.trustStorePassword:}")
    private String trustStorePassword;

    @Bean
    public KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<String, String>> statisticsListenerContainerFactory() {
        ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(consumerFactory());
        factory.setConcurrency(1);
        factory.setBatchListener(true);
        factory.getContainerProperties().setPollTimeout(1500);
        return factory;
    }

    public ConsumerFactory<String, String> consumerFactory() {
        return new DefaultKafkaConsumerFactory<>(consumerConfigs());
    }

    public Map<String, Object> consumerConfigs() {
        Map<String, Object> propsMap = new HashMap<>();
        propsMap.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, servers);
        propsMap.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, enableAutoCommit);
        propsMap.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, autoCommitInterval);
        propsMap.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, sessionTimeout);
        propsMap.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        propsMap.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        propsMap.put(ConsumerConfig.GROUP_ID_CONFIG, groupId);
        propsMap.put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, 1);//每一批数量
        propsMap.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, autoOffsetReset);
        //TODO kafka不做认证
        //propsMap.putAll(KafkaKeyManage.getSslMap(servers, keyStoreUrl, keyStorePassword, keyPassword, trustStoreUrl, trustStorePassword));
        return propsMap;
    }

}
